package pl.th.permission;

import android.content.Intent;
import android.os.Build;
import android.provider.Settings;

/**
 * request the permission {@link android.Manifest.permission#MANAGE_EXTERNAL_STORAGE}
 */
public class StoragePerRProcessor extends AbstractProcessor{

    public StoragePerRProcessor(String... permissions) {
        super(permissions);
    }

    @Override
    @SuppressWarnings({"ConstantConditions", "InlinedApi"})
    public void process(ProxyFragment proxy) {
        if (Utils.isAndroidRAndUp(proxy.osVersion) && Utils.isAndroidRAndUp(proxy.targetVersion)) {
            if (PermissionCompat.isManageExtStorageGranted(proxy.getContext())) {
                proxy.workOnNextProcessor();
            } else {
                try {
                    Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
                    intent.setData(Utils.buildPackageUri(proxy.getContext()));
                    proxy.startActivityForResult(intent);
                } catch (Exception e) {
                    proxy.startActivityForResult(PermissionCompat.buildAppDetailIntent(proxy.getContext()));
                }
            }
        } else {
            proxy.workOnNextProcessor();
        }
    }
}
